﻿using System;
using System.Collections.Generic;
using System.Linq;
using eCRAM.Model;

namespace eCRAM.Dao
{
    public class CramAttachmentRepository
    {
        #region CramAttachment

        /// <summary>
        /// Récupère toutes les pièces jointes
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<Cram_Attachment> GetAll()
        {
            using (var context = new eCramDataContext())
            {
                return context.Cram_Attachments.ToList();
            }
        }

        /// <summary>
        /// Récupère les pièce jointes d'un cram
        /// </summary>
        /// <param name="cramId"></param>
        /// <returns></returns>
        public static IEnumerable<Cram_Attachment> GetByCramId(Guid cramId)
        {
            using (var context = new eCramDataContext())
            {
                return context.Cram_Attachments.Where(cl => cl.CramId == cramId).ToList();
            }
        }

        public static void Create(Cram_Attachment cramAttachment)
        {
            using (var context = new eCramDataContext())
            {
                context.Cram_Attachments.InsertOnSubmit(cramAttachment);
                context.Cram_AttachmentDatas.InsertOnSubmit(cramAttachment.Cram_AttachmentData);
                context.SubmitChanges();
            }
        }

        public static void InsertAttachments(IEnumerable<Cram_Attachment> cramAttachments)
        {
            foreach (var cramAttachment in cramAttachments)
            {
                Create(cramAttachment);
            }
        }

        public static void Delete(Cram_Attachment cramAttachment)
        {
            using (var context = new eCramDataContext())
            {
                Cram_Attachment originalAttachment = context.Cram_Attachments.FirstOrDefault(att => att.Id == cramAttachment.Id);
                Cram_AttachmentData originalAttachmentData =
                    context.Cram_AttachmentDatas.FirstOrDefault(data => data.AttachementId == cramAttachment.Id);
                if (originalAttachment != null)
                {
                    context.Cram_Attachments.DeleteOnSubmit(originalAttachment);
                    context.Cram_AttachmentDatas.DeleteOnSubmit(originalAttachmentData);
                    context.SubmitChanges();
                }
            }
        }

        public static void DeleteByCramId(Guid cramId)
        {
            using (var context = new eCramDataContext())
            {
                foreach (var originalCramAttachmentDatas in context.Cram_AttachmentDatas.Where(cl => cl.Cram_Attachment.CramId == cramId))
                {
                    context.Cram_AttachmentDatas.DeleteOnSubmit(originalCramAttachmentDatas);
                }
                foreach (var originalCramAttachment in context.Cram_Attachments.Where(cl => cl.CramId == cramId))
                {
                    context.Cram_Attachments.DeleteOnSubmit(originalCramAttachment);
                }
                context.SubmitChanges();
            }
        }

        /// <summary>
        /// Récupère une pièce jointe à partir de son identifiant
        /// </summary>
        /// <param name="attachmentId"></param>
        /// <returns></returns>
        public static Cram_Attachment GetById(Guid attachmentId)
        {
            using (var context = new eCramDataContext())
            {
                return (context.Cram_Attachments.FirstOrDefault(p => p.Id == attachmentId));
            }
        }

        #endregion

        #region CramAttachmentDatas

        /// <summary>
        /// récupère les données d'un pièce jointe
        /// </summary>
        /// <param name="attachmentDataId"></param>
        /// <returns></returns>
        public static Cram_AttachmentData GetByAttachmentDataById(Guid attachmentDataId)
        {
            using (var context = new eCramDataContext())
            {
                return (context.Cram_AttachmentDatas.FirstOrDefault(p => p.AttachementId == attachmentDataId));
            }
        }

        /// <summary>
        /// Insère les données d'un pièce jointe
        /// </summary>
        /// <param name="cramAttachmentData"></param>
        public static void Create(Cram_AttachmentData cramAttachmentData)
        {
            using (var context = new eCramDataContext())
            {
                context.Cram_AttachmentDatas.InsertOnSubmit(cramAttachmentData);
                context.SubmitChanges();
            }
        }

        

        public static void Delete(Cram_AttachmentData cramAttachmentData)
        {
            using (var context = new eCramDataContext())
            {
                Cram_AttachmentData originalAttachmentData = context.Cram_AttachmentDatas.FirstOrDefault(cl => cl.AttachementId == cramAttachmentData.AttachementId);
                if (originalAttachmentData != null)
                {
                    context.Cram_AttachmentDatas.DeleteOnSubmit(originalAttachmentData);
                    context.SubmitChanges();
                }
            }
        }
        #endregion
    }
}
